package com.jian.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jian.project.common.ErrorCode;
import com.jian.project.exception.BusinessException;
import com.jian.project.mapper.TbUserInterfaceMapper;
import com.jian.project.model.dto.tbuserinterface.TbUserInterfaceInvokeRequest;
import com.jian.project.model.entity.TbUserInterface;
import com.jian.project.model.enums.UserInterfaceStatusEnum;
import com.jian.project.model.vo.InterfaceLeftNumberVo;
import com.jian.project.model.vo.UserInterfaceVO;
import com.jian.project.service.TbUserInterfaceService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 18214
 * @description 针对表【tb_user_interface(用户接口调用关系)】的数据库操作Service实现
 * @createDate 2023-02-22 09:23:12
 */
@Service
public class TbUserInterfaceServiceImpl extends ServiceImpl<TbUserInterfaceMapper, TbUserInterface>
        implements TbUserInterfaceService {

    @Resource
    private TbUserInterfaceMapper tbUserInterfaceMapper;

    @Override
    public void validTbUserInterface(TbUserInterface tbUserInterface, boolean add) {
        if (tbUserInterface == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = tbUserInterface.getId();
        Long userId = tbUserInterface.getUserId();
        Long interfaceId = tbUserInterface.getInterfaceId();
        Long totalNum = tbUserInterface.getTotalNum();
        Integer leftNum = tbUserInterface.getLeftNum();
        Integer status = tbUserInterface.getStatus();
        Date createTime = tbUserInterface.getCreateTime();
        Date updateTime = tbUserInterface.getUpdateTime();
        Integer isDelete = tbUserInterface.getIsDelete();

        // 创建时，所有参数必须非空
        if (add) {
            if (ObjectUtils.anyNotNull(id, status, createTime, updateTime, isDelete)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "新增字段有误");
            }
        }

        if (interfaceId == null || interfaceId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口id错误,不能为空或者小于0");
        }
    }

    @Override
    public Boolean invokeApi(TbUserInterfaceInvokeRequest request) {
        if (request == null || request.getUserId() == null || request.getInterfaceId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户id或者接口id不能为空");
        }

        if (request.getUserId() <= 0 || request.getInterfaceId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户id或者接口id不能为等于小于0");
        }
        TbUserInterface tbUserInterface = new TbUserInterface();
        BeanUtils.copyProperties(request, tbUserInterface);

        UpdateWrapper<TbUserInterface> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", tbUserInterface.getUserId());
        updateWrapper.eq("interface_id", tbUserInterface.getInterfaceId());
        updateWrapper.eq("status", UserInterfaceStatusEnum.NORMAL.getValue());
        updateWrapper.gt("left_num", 0);

        updateWrapper.setSql("total_num = total_num + 1, left_num = left_num - 1");

        if (tbUserInterfaceMapper.update(null, updateWrapper) == 0)
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "剩余次数不足,调用失败");
        return true;
    }

    @Override
    public List<UserInterfaceVO> invokeTotal() {
        return invokeTotal(3);
    }

    @Override
    public InterfaceLeftNumberVo getLeftNumber(TbUserInterfaceInvokeRequest tbUserInterfaceQueryRequest) {
        if (tbUserInterfaceQueryRequest == null || tbUserInterfaceQueryRequest.getUserId() == null || tbUserInterfaceQueryRequest.getInterfaceId() == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);

        QueryWrapper<TbUserInterface> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", tbUserInterfaceQueryRequest.getUserId());
        queryWrapper.eq("interface_id", tbUserInterfaceQueryRequest.getInterfaceId());

        TbUserInterface tbUserInterface = tbUserInterfaceMapper.selectOne(queryWrapper);
        if (tbUserInterface == null)
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        InterfaceLeftNumberVo interfaceLeftNumberVo = new InterfaceLeftNumberVo();
        BeanUtils.copyProperties(tbUserInterface, interfaceLeftNumberVo);
        return interfaceLeftNumberVo;
    }

    @Override
    public List<UserInterfaceVO> invokeTotal(Integer top) {
        return tbUserInterfaceMapper.invokeTotal(top);
    }
}




